package com.example.egmvc1.controller;

import com.example.egmvc1.jovo.dto.AddUserParam;
import com.example.egmvc1.jovo.dto.UpdateParam;
import com.example.egmvc1.jovo.entity.User;
import com.example.egmvc1.jovo.vo.UserListVO;
import com.example.egmvc1.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    UserMapper userMapper;


    @PostMapping("add")
    public String add(AddUserParam addUserParam) {
        User user = new User();
        BeanUtils.copyProperties(addUserParam, user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        int insert = userMapper.insert(user);
        if (insert > 0) {
            return "insert successfully";
        } else {
            return "insert failed";
        }
    }

    @GetMapping("list")
    public List<UserListVO> selectList()
    {
        return userMapper.selectALL();
    }

    @PostMapping("del")
    public String del(Long id)
    {
        if (userMapper.delById(id)>0 )
            return "delete successfully";
        return "delete failed";
    }

    @PostMapping("update")
    public String updateUser(UpdateParam updateParam)
    {
        User user = new User();
        BeanUtils.copyProperties(updateParam,user);
        user.setUpdateTime(new Date());
        if(userMapper.updateuser(user) > 0)
            return "update successfully";
        return "update failed";
    }


}
